// Copyright (c) 2011 Blue Onion Software, All rights reserved
using System;
using System.ComponentModel;
using System.Windows.Forms;
using BlogGenerator.Properties;
using BlueOnionSoftware.Bloget;

namespace BlogGenerator
{
    public partial class BlogGenerator : Form
    {
        public BlogGenerator(string[] cmdArguments)
        {
            InitializeComponent();

            startDate.Value = DateTime.Today.AddYears(-3);
            endDate.Value = DateTime.Today;
            minPosts.Value = 0;
            maxPosts.Value = 6;

            if (cmdArguments.Length > 0)
            {
                folderTextbox.Text = cmdArguments[0];
                GenerateClick(null, EventArgs.Empty);

                var timer = new Timer {Interval = 1000};
                timer.Tick += TimerTick;
                timer.Start();
            }
        }

        private void TimerTick(object sender, EventArgs e)
        {
            Close();
        }

        private void BrowseButtonClick(object sender, EventArgs e)
        {
            var save = new SaveFileDialog();

            if (save.ShowDialog(this) == DialogResult.OK)
            {
                folderTextbox.Text = save.FileName;
            }
        }

        private void GenerateClick(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;

                var blog = new Blog
                {
                    Title = "Bloget Sample Blog",
                    Description = "Generated by BlogGenerator",
                    Webmaster = "webmaster@example.com",
                    LicenseAgreement = true,
                    Link = "http://example.com/blog.aspx",
                    DataContext = folderTextbox.Text,
                    EnableMetaWeblogApi = true,
                    EnableRealSimpleDiscovery = true,
                    EnablePingBackService = true,
                    EnableAutoPingBack = true
                };

                var date = startDate.Value;
                var min = (int) minPosts.Value;
                var max = (int) maxPosts.Value;
                var random = new Random();
                var postCounter = 1;
                var contentText = Resources.LorumIpsum;

                while (date < endDate.Value)
                {
                    var posts = Math.Max(min, random.Next(max + 1));
                    var day = 1;

                    while (posts-- > 0)
                    {
                        var post = new Post
                        {
                            Title = string.Format("Sample Post {0}", postCounter++),
                            Created = date.AddDays(Math.Min(day++, 28))
                        };
                        var content = new Content
                        {
                            Text = contentText,
                            LastUpdate = post.Created
                        };
                        blog.SaveContent(post.Id, content);
                        blog.Posts.Insert(0, post);
                    }

                    date = date.AddMonths(1);
                }

                blog.Save();
            }

            finally
            {
                Cursor = Cursors.Default;
            }
        }

        private void FolderTextboxValidating(object sender, CancelEventArgs e)
        {
            e.Cancel = string.IsNullOrEmpty(folderTextbox.Text);
        }
    }
}